package com.example.mycityapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RegisterPage extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_page);
		

		Button b2 = (Button) findViewById(R.id.registercancelbutton);
		b2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				RegisterPage.this.finish();
			}
		});
		
		Button b = (Button) findViewById(R.id.registerregisterbutton);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText usernameEditText = (EditText) findViewById(R.id.registerusername);
				EditText password1EditText = (EditText) findViewById(R.id.registerpassword1);
				EditText password2EditText = (EditText) findViewById(R.id.registerpassword2);
				EditText emailEditText = (EditText) findViewById(R.id.registeremail);
				EditText firstnameEditText = (EditText) findViewById(R.id.registerfirstname);
				EditText lastnameEditText = (EditText) findViewById(R.id.registerlastname);
				String username = usernameEditText.getText().toString();
				String password1 = password1EditText.getText().toString();
				String password2 = password2EditText.getText().toString();
				String email = emailEditText.getText().toString();
				String firstname = firstnameEditText.getText().toString();
				String lastname = lastnameEditText.getText().toString();
				
				String url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/usernameAvailable?username=" + username;
				String urlResponse = JSONParser.getJSONFromUrl(url);
				
				if (username.equals("") || password1.equals("") || password2.equals("") || email.equals("") || firstname.equals("") || lastname.equals(""))
				{
					AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(RegisterPage.this);
					
                    dlgAlert.setMessage("There are empty fields.");
                    dlgAlert.setTitle("Error Message...");
                    dlgAlert.setPositiveButton("OK", null);
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();

                    dlgAlert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                        }
                    });
				}
				else
				{
					if (urlResponse.contains("true"))
					{
						url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/emailAvailable?email=" + email;
						urlResponse = JSONParser.getJSONFromUrl(url);
						if (urlResponse.contains("true"))
						{
							if (password1.equals(password2))
							{
								url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/register?username="
									+username+"&name="+firstname+"&surname="+lastname+"&email="+email+"&password="+password1;
								urlResponse = JSONParser.getJSONFromUrl(url);
								
			                    //RegisterPage.this.finish();
								AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(RegisterPage.this);
								
			                    dlgAlert.setMessage("You are successfully registered. Please go back and login");
			                    dlgAlert.setTitle("Success Message");
			                    dlgAlert.setPositiveButton("OK", null);
			                    dlgAlert.setCancelable(true);
			                    dlgAlert.create().show();
	
			                    dlgAlert.setPositiveButton("Ok",
			                        new DialogInterface.OnClickListener() {
			                            public void onClick(DialogInterface dialog, int which) {
	
			                        }
			                    });
							}
							else
							{
								AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(RegisterPage.this);
	
			                    dlgAlert.setMessage("Passwords don't match.");
			                    dlgAlert.setTitle("Error Message...");
			                    dlgAlert.setPositiveButton("OK", null);
			                    dlgAlert.setCancelable(true);
			                    dlgAlert.create().show();
	
			                    dlgAlert.setPositiveButton("Ok",
			                        new DialogInterface.OnClickListener() {
			                            public void onClick(DialogInterface dialog, int which) {
	
			                        }
			                    });
							}
						}
						else
						{
							Log.i("",urlResponse);
							AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(RegisterPage.this);
	
		                    dlgAlert.setMessage("Email is invalid or already registered.");
		                    dlgAlert.setTitle("Error Message...");
		                    dlgAlert.setPositiveButton("OK", null);
		                    dlgAlert.setCancelable(true);
		                    dlgAlert.create().show();
	
		                    dlgAlert.setPositiveButton("Ok",
		                        new DialogInterface.OnClickListener() {
		                            public void onClick(DialogInterface dialog, int which) {
	
		                        }
		                    });
						}
					}
					else
					{
						AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(RegisterPage.this);
	
	                    dlgAlert.setMessage("User name has been taken.");
	                    dlgAlert.setTitle("Error Message...");
	                    dlgAlert.setPositiveButton("OK", null);
	                    dlgAlert.setCancelable(true);
	                    dlgAlert.create().show();
	
	                    dlgAlert.setPositiveButton("Ok",
	                        new DialogInterface.OnClickListener() {
	                            public void onClick(DialogInterface dialog, int which) {
	
	                        }
	                    });
					}
				}
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register_page, menu);
		return true;
	}

}
